{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:44:42.711237800Z",
     "start_time": "2024-01-19T13:44:40.859635900Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import platform\n",
    "from PIL import Image\n",
    "from torchvision.transforms import ToTensor\n",
    "platform = platform.system()\n",
    "if platform == 'Linux':\n",
    "    !BASICSR_JIT=True\n",
    "else:\n",
    "    !set BASICSR_JIT=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "from basicsr.losses.losses import L1Loss, LPIPSLoss, MSSSIMLoss"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:44:47.118204500Z",
     "start_time": "2024-01-19T13:44:47.116697200Z"
    }
   },
   "id": "46789a5f6958c1ad"
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\earthyzinc\\Programs\\miniconda\\Lib\\site-packages\\torchvision\\models\\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.\n",
      "  warnings.warn(\n",
      "C:\\Users\\earthyzinc\\Programs\\miniconda\\Lib\\site-packages\\torchvision\\models\\_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=VGG16_Weights.IMAGENET1K_V1`. You can also use `weights=VGG16_Weights.DEFAULT` to get the most up-to-date weights.\n",
      "  warnings.warn(msg)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading pretrained model LPIPS from C:\\Users\\earthyzinc\\.cache\\torch\\hub\\checkpoints\\LPIPS_v0.1_vgg-a78928a0.pth\n"
     ]
    }
   ],
   "source": [
    "l1_loss = L1Loss()\n",
    "lpips_loss = LPIPSLoss()\n",
    "msssim_loss = MSSSIMLoss()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:44:49.223427900Z",
     "start_time": "2024-01-19T13:44:47.929835800Z"
    }
   },
   "id": "cf4b9b80f7af5f0e"
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "high_quality_image = ToTensor()(Image.open('D:/DeepLearning/dataset/Fuse_data/train1/GT/0_01.jpg').convert('RGB'))[None, ::]\n",
    "low_quality_image = ToTensor()(Image.open('D:/DeepLearning/dataset/Fuse_data/train1/IN/0_01.jpg').convert('RGB'))[None, ::]\n",
    "low_quality_image_2 = ToTensor()(Image.open('D:/DeepLearning/dataset/Fuse_data/train1/IN/0_02.jpg').convert('RGB'))[None, ::]\n",
    "random_data = torch.randn(1, 3, high_quality_image.shape[2], high_quality_image.shape[3])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:44:51.456882200Z",
     "start_time": "2024-01-19T13:44:51.398997600Z"
    }
   },
   "id": "aad0a426102eb39e"
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(0.)\n",
      "tensor(0.1478)\n",
      "tensor(0.2137)\n",
      "tensor(0.9024)\n",
      "(tensor(0., device='cuda:0', grad_fn=<MulBackward0>), None)\n",
      "(tensor(0.2986, device='cuda:0', grad_fn=<MulBackward0>), None)\n",
      "(tensor(0.6973, device='cuda:0', grad_fn=<MulBackward0>), None)\n",
      "(tensor(0.9259, device='cuda:0', grad_fn=<MulBackward0>), None)\n",
      "tensor(0.)\n",
      "tensor(0.3908)\n",
      "tensor(0.8614)\n",
      "tensor(0.9996)\n"
     ]
    }
   ],
   "source": [
    "print(l1_loss(high_quality_image, high_quality_image))\n",
    "print(l1_loss(high_quality_image, low_quality_image))\n",
    "print(l1_loss(high_quality_image, low_quality_image_2))\n",
    "print(l1_loss(high_quality_image, random_data))\n",
    "\n",
    "print(lpips_loss(high_quality_image, high_quality_image))\n",
    "print(lpips_loss(high_quality_image, low_quality_image))\n",
    "print(lpips_loss(high_quality_image, low_quality_image_2))\n",
    "print(lpips_loss(high_quality_image, random_data))\n",
    "\n",
    "print(msssim_loss(high_quality_image, high_quality_image))\n",
    "print(msssim_loss(low_quality_image, high_quality_image))\n",
    "print(msssim_loss(high_quality_image, low_quality_image_2))\n",
    "print(msssim_loss(high_quality_image, random_data))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:45:54.025654500Z",
     "start_time": "2024-01-19T13:44:52.618949600Z"
    }
   },
   "id": "492935a20343693c"
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading pretrained model LPIPS from C:\\Users\\earthyzinc\\.cache\\torch\\hub\\checkpoints\\LPIPS_v0.1_vgg-a78928a0.pth\n",
      "(tensor(0.2702, device='cuda:0', grad_fn=<MulBackward0>), None)\n",
      "(tensor(0.7994, device='cuda:0', grad_fn=<MulBackward0>), None)\n",
      "(tensor(0., device='cuda:0', grad_fn=<MulBackward0>), None)\n"
     ]
    }
   ],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T11:45:23.043744Z",
     "start_time": "2024-01-19T11:45:22.303232200Z"
    }
   },
   "id": "4f3a9ba04efe9cbf"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "83f4d46194a22a1f"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
